package com.briup.shopadmin.service.impl;

import com.briup.shopadmin.dao.ICategoryDao;
import com.briup.shopadmin.dao.IOrderDao;
import com.briup.shopadmin.dao.IShopDao;
import com.briup.shopadmin.dao.IUserDao;
import com.briup.shopadmin.service.IIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class IndexServiceImpl implements IIndexService {
    @Autowired
    IUserDao userDao;
    @Autowired
    IShopDao shopDao;
    @Autowired
    ICategoryDao categoryDao;
    @Autowired
    IOrderDao orderDao;

    @Override
    public Map<String, Long> dashboard() {
        HashMap<String, Long> map = new HashMap<>();

        long userNum = userDao.count();
        long shopNum = shopDao.count();
        long categoryNum = categoryDao.countByParentIdIsNotNull();
        long orderNum = orderDao.count();
        map.put("categoryNum", categoryNum);
        map.put("shopNum", shopNum);
        map.put("userNum", userNum);
        map.put("orderNum", orderNum);

        return map;
    }

    @Override
    public Map<String, Double> sexRatio() {
        HashMap<String, Double> map = new HashMap<>();
//        用户总数
        double sum = userDao.count();
//        男总数
        double mSum = userDao.countByGender("男");
//        女总数
        double fSum = userDao.countByGender("女");
//        未知性别=用户总数-男总数-女总数
        double uSum = sum - mSum - fSum;

        double mVal = (mSum == 0.0) ? 0.0 : mSum / sum;
        double fVal = (fSum == 0.0) ? 0.0 : fSum / sum;
        double uVal = (uSum == 0.0) ? 0.0 : uSum / sum;
        map.put("男", mVal);
        map.put("女", fVal);
        map.put("未知", uVal);
        return map;
    }

    @Override
    public Map<String, String> categoryStats() {
        HashMap<String, String> map = new HashMap<>();
        List<String[]> list = shopDao.countByCategory();
        for (String[] arr : list) {
                map.put(arr[0],arr[1]);
        }
        return  map;
    }


}
